<!DOCTYPE html>
<html>
<head>
    <title>Hall Effect Diagram</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f0f0f0;
        }
        canvas {
            border: 1px solid #ccc;
            background-color: #fff;
        }
    </style>
</head>
<body>
    <canvas id="physicsCanvas" width="600" height="500"></canvas>
    <script>
        const canvas = document.getElementById('physicsCanvas');
        const ctx = canvas.getContext('2d');

        // Helper function to draw an arrow
        function drawArrow(fromx, fromy, tox, toy) {
            const headlen = 10; // length of head in pixels
            const dx = tox - fromx;
            const dy = toy - fromy;
            const angle = Math.atan2(dy, dx);
            ctx.beginPath();
            ctx.moveTo(fromx, fromy);
            ctx.lineTo(tox, toy);
            ctx.lineTo(tox - headlen * Math.cos(angle - Math.PI / 6), toy - headlen * Math.sin(angle - Math.PI / 6));
            ctx.moveTo(tox, toy);
            ctx.lineTo(tox - headlen * Math.cos(angle + Math.PI / 6), toy - headlen * Math.sin(angle + Math.PI / 6));
            ctx.stroke();
        }

        // --- Style settings ---
        ctx.strokeStyle = 'black';
        ctx.fillStyle = 'black';
        ctx.lineWidth = 2;
        ctx.font = '18px Arial';
        const grayFill = '#D3D3D3';

        // --- Define coordinates for the cuboid ---
        const P = { x: 150, y: 400 };
        const Q = { x: 350, y: 400 };
        const C = { x: 100, y: 250 };
        const D = { x: 300, y: 250 };
        const E = { x: 450, y: 150 };
        const F = { x: 250, y: 150 };
        // Calculate back-bottom-right vertex for shading
        const R_vtx = { x: Q.x + (E.x - D.x), y: Q.y + (E.y - D.y) };

        // --- Draw shaded faces ---
        ctx.fillStyle = grayFill;

        // Shade front face (CDQP)
        ctx.beginPath();
        ctx.moveTo(C.x, C.y);
        ctx.lineTo(D.x, D.y);
        ctx.lineTo(Q.x, Q.y);
        ctx.lineTo(P.x, P.y);
        ctx.closePath();
        ctx.fill();

        // Shade right face (DEQR_vtx)
        ctx.beginPath();
        ctx.moveTo(D.x, D.y);
        ctx.lineTo(E.x, E.y);
        ctx.lineTo(R_vtx.x, R_vtx.y);
        ctx.lineTo(Q.x, Q.y);
        ctx.closePath();
        ctx.fill();
        
        // --- Draw cuboid outlines ---
        ctx.beginPath();
        // Front face
        ctx.moveTo(P.x, P.y);
        ctx.lineTo(Q.x, Q.y);
        ctx.lineTo(D.x, D.y);
        ctx.lineTo(C.x, C.y);
        ctx.lineTo(P.x, P.y);
        // Top face
        ctx.moveTo(C.x, C.y);
        ctx.lineTo(D.x, D.y);
        ctx.lineTo(E.x, E.y);
        ctx.lineTo(F.x, F.y);
        // Right edge
        ctx.moveTo(D.x, D.y);
        ctx.lineTo(E.x, E.y);
        ctx.stroke();

        // Dashed edge FC
        ctx.beginPath();
        ctx.setLineDash([5, 5]);
        ctx.moveTo(F.x, F.y);
        ctx.lineTo(C.x, C.y);
        ctx.stroke();

        // --- Draw dashed guide lines on top surface ---
        const S = { x: F.x + 0.6 * (C.x - F.x), y: F.y + 0.6 * (C.y - F.y) };
        const R = { x: E.x + 0.6 * (D.x - E.x), y: E.y + 0.6 * (D.y - E.y) };

        const S_back = { x: S.x + (F.x-C.x)*0.5, y: S.y + (F.y-C.y)*0.5 };
        const R_back = { x: R.x + (E.x-D.x)*0.5, y: R.y + (E.y-D.y)*0.5 };
        
        // Dashed rectangle inside
        const S_on_FC = { x: F.x + 0.4 * (C.x - F.x), y: F.y + 0.4 * (C.y - F.y)};
        const R_on_DE = { x: D.x + 0.4 * (E.x - D.x), y: D.y + 0.4 * (E.y - D.y)};
        
        ctx.beginPath();
        ctx.moveTo(S_on_FC.x, S_on_FC.y);
        ctx.lineTo(R_on_DE.x, R_on_DE.y);

        const S_back_pt = { x: S_on_FC.x + (E.x - D.x) * 0.7, y: S_on_FC.y + (E.y - D.y) * 0.7 };
        ctx.moveTo(S_on_FC.x, S_on_FC.y);
        ctx.lineTo(S_back_pt.x, S_back_pt.y);
        ctx.stroke();
        
        // Reset to solid lines
        ctx.setLineDash([]);

        // --- Draw Magnetic Field (B) ---
        drawArrow(300, 100, 300, 220);
        drawArrow(380, 130, 380, 250);
        ctx.fillText('magnetic field', 270, 60);
        ctx.fillText('flux density', 275, 80);
        ctx.font = 'italic bold 18px Arial';
        ctx.fillText('B', 390, 80);
        ctx.font = '18px Arial';


        // --- Draw Current (I) ---
        drawArrow(20, 410, P.x - 20, 410);
        drawArrow(40, 430, P.x, 430);
        ctx.fillText('current', 40, 460);
        ctx.font = 'italic bold 18px Arial';
        ctx.fillText('I', 110, 460);
        ctx.font = '18px Arial';

        // --- Add Labels ---
        ctx.fillStyle = 'black';
        ctx.fillText('C', C.x - 20, C.y + 5);
        ctx.fillText('D', D.x + 10, D.y + 5);
        ctx.fillText('E', E.x + 10, E.y + 5);
        ctx.fillText('F', F.x - 20, F.y + 5);
        ctx.fillText('P', P.x - 20, P.y + 5);
        ctx.fillText('Q', Q.x + 5, Q.y + 5);
        ctx.fillText('S', S_on_FC.x - 20, S_on_FC.y + 10);
        ctx.fillText('R', R_on_DE.x + 5, R_on_DE.y + 10);
        
        ctx.fillText('Fig. 8.1', 260, 480);
    </script>
</body>
</html>